import mutationTypes from './mutation-types'

const mutations = {

  // 修改播放状态
  [mutationTypes.MARK_VIDEO_PLAYED](state, playload) {
    // 改变state
    state.isPlayed = playload.isPlayed
    // 设置storage
    window.localStorage.isPlayed = JSON.stringify(playload.isPlayed)
  },

  // 修改播放状态
  [mutationTypes.SET_PLAY_STATUS](state, status) {
    state.isPlayed = status
  },

  // 初始化购物车状态
  [mutationTypes.SET_CART_STATUS](state, cart) {
    state.cart = cart
  },

  // 添加商品到购物车
  [mutationTypes.ADD_PROD_TO_CART](state, product) {
    state.cart.push({
      id: product.id,
      prodName: product.prodName,
      desc: product.desc,
      thumb: product.thumb,
      price: product.price,
      quantity: 1
    })
    window.localStorage.cart = JSON.stringify(state.cart)
  },

  // 商品数量累加
  [mutationTypes.INCREMENT_PROD_QUANTITY](state, product) {
    const cartItem = state.cart.find(item => item.id === product.id)
    cartItem.quantity++
    window.localStorage.cart = JSON.stringify(state.cart)
  },

  // 增加或减少商品数量
  [mutationTypes.UPDATE_PROD_QUANTITY](state, product) {
    const cartItem = state.cart.find(item => item.id === product.id)
    cartItem.quantity = product.quantity
    window.localStorage.cart = JSON.stringify(state.cart)
  }

}

export default mutations